var form, $,areaData;
layui.use(['form','layer','upload','laydate','selectN'],function(){
    form = layui.form;
    $ = layui.jquery;
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        laydate = layui.laydate,
        selectN=layui.selectN;

    var date=new Date();var year = date.getFullYear();
    let years = [];
    for(let i=year;i>=2000;i--){
        years.push({"id":`${i}`,"name":`${i}`+"级"});
    }
    var catData = [{"id":1,"name":"教师"},{"id":2,"name":"学生","children":years}];
    //无限级分类-所有配置
    var catIns = null;

//通过js动态选择
//     $('.set1').click(function(){
//         catIns1.set([6,10]);
//
//     });

    //监听重置按钮
    $('form').find(':reset').click(function(){
        $('form')[0].reset();
        $("#roleName").val(tempRole);//重置后身份不能改变
        catIns.set();//默认值
        return false;
    });


    var tempRole="visitor";

    //回显用户个人信息
    var _ticket = $.cookie("token");

var tempLoginName="";
    if (_ticket != null && _ticket !== "") {
        //确认ip
        $.ajax({
            url: "/admin/user/getIp",//单点登录链接
            type: "GET",
            success: function (data) {
                var userT = JSON.parse(_ticket);
                if (data != userT.loginIp) {
                    layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {
                    });
                    window.location.href = "/login";
                }
                else{
                $.ajax({
                    url: '/admin/user/' + userT.id,
                    type: 'GET',
                    success: function (user) {
                        $("#id").val(user.id);
                        $("#loginName").val(user.loginName);
                        tempLoginName=user.loginName;
                        $("#realname").val(user.realname);

                        $("#sex input[value=" + user.sex + "]").prop("checked", true);
                        form.render('radio');
                        $("#mobile").val(user.mobile);
                        $("#email").val(user.email);
                        $("#roleId").val(user.roleId);
                        $.ajax({
                            url: '/admin/role/' + user.roleId,
                            type: 'GET',
                            success: function (da) {

                                if (da != null) {
                                    $("#roleName").val(da.name);
                                }
                                tempRole=da.name;
                            }
                        })


                        catIns=selectN({
                            //元素容器【必填】
                            elem: '#realname'
                            //候选数据【必填】
                            ,data: catData
                            //数据的键名
                            // ,field:{idName:'id',realName:'name',age:'children'}
                            //空值项提示，可设置为数组['请选择省','请选择市','请选择县']
                            ,tips: ['请选择','请选择年级']
                            ,selected:user.realname==1?user.realname:[user.realname,user.age]
                            //设置了长度
                            // ,width:null
                            // //默认值
                            // ,selected: [6,10]
                            // //为真只取最后一个值
                            // ,last:true
                            //
                            // //事件过滤器，lay-filter名 不设置与选择器相同(去#.)
                            // ,filter: ''
                            //
                            // //input的name 不设置与选择器相同(去#.)
                            // ,name: 'cat2'
                            //
                            // //数据分隔符
                            // ,delimiter: ','
                            //表单区分 form.render(type, filter); 为class="layui-form" 所在元素的 lay-filter="" 的值
                            // ,formFilter: null

                        });

                    }
                })
                }
            }
        });
    } else {
        layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {
        });
        window.location.href = "/login";
    }

    //提交个人资料
    form.on("submit(changeUser)",function(data){

        if(data.field.loginName!=tempLoginName) {
            $.ajax({
                url: "/verifyUserName",
                type: "POST",
                data: {loginName: data.field.loginName},
                success: function (d) {
                    if (d.code != 200) {


                        //处理realname数据
                        var ids = data.field.realname.split(",");
                        if (ids.length != 0) {
                            data.field.realname = ids[0];
                            if (ids.length == 2)
                                data.field.age = ids[1];
                            else
                                data.field.age = "";
                        }
                        else
                            data.field.realname = "";

                        var index = layer.msg('提交中，请稍候', {icon: 16, time: false, shade: 0.8});
                        // 更新数据库
                        $.ajax({
                            url: '/admin/user/edit',
                            type: "POST",
                            data: data.field,
                            success: function (data) {
                                if (data.code == 200) {
                                    layer.close(index);
                                    layer.msg("修改成功！");
                                    location.reload();
                                } else {
                                    layer.close(index);
                                    layer.msg("修改失败！请重试");
                                    location.reload();
                                }
                            },
                            error: function () {
                                layer.close(index);
                                layer.msg("无法连接服务器，请重试");
                            }
                        });
                        //更新cookie缓存数据
                    } else {
                        layer.msg('修改失败！该用户名已存在！');

                    }
                }
            })
        }else {

            //处理realname数据
            var ids = data.field.realname.split(",");
            if (ids.length != 0) {
                data.field.realname = ids[0];
                if (ids.length == 2)
                    data.field.age = ids[1];
                else
                    data.field.age = "";
            }
            else
                data.field.realname = "";

            var index = layer.msg('提交中，请稍候', {icon: 16, time: false, shade: 0.8});
            // 更新数据库
            $.ajax({
                url: '/admin/user/edit',
                type: "POST",
                data: data.field,
                success: function (data) {
                    if (data.code == 200) {
                        layer.close(index);
                        layer.msg("修改成功！");
                        location.reload();
                    } else {
                        layer.close(index);
                        layer.msg("修改失败！请重试");
                        location.reload();
                    }
                },
                error: function () {
                    layer.close(index);
                    layer.msg("无法连接服务器，请重试");
                }
            });
            //更新cookie缓存数据
        }




        return false;
    });


});